﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using System.IO;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using Data;

namespace GlobalCode
{
    public partial class GlobalCodeEdit : UserControl
    {
        public GlobalCodeEdit()
        {
            InitializeComponent();
        }

        private TreeListNode tn = null;
        private codelist cla = null;
        private XtraTabPage xt = null;
        public GlobalCodeEdit(TreeListNode _tn, codelist _cla,XtraTabPage _xt)
        {
            this.tn = _tn;
            this.cla = _cla;
            this.xt = _xt;
            InitializeComponent();
            this.textEdit1.Text = _cla.Key;
            this.fastColoredTextBox1.Text = _cla.Data;
            Code.Codehinting(this.fastColoredTextBox1);
        }

        Codestyle.Codestyle Code = new Codestyle.Codestyle();



        private void fastColoredTextBox1_TextChanged(object sender, FastColoredTextBoxNS.TextChangedEventArgs e)
        {
            Code.StyleRun(sender, e);
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton_save_Click(object sender, EventArgs e)
        {
            codelist cl = new codelist()
            {
                Key = this.textEdit1.Text,
                Data = this.fastColoredTextBox1.Text
            };
            //保存节点
            string thisExeFile = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string oldthisFile = thisExeFile + GlobalDirectory.SaveDirectory + "\\JSONCodeDataList\\" + cla.Key + ".json";
            string thisFile = thisExeFile + GlobalDirectory.SaveDirectory + "\\JSONCodeDataList\\" + this.textEdit1.Text + ".json";
            if (cla.Key != this.textEdit1.Text)
            {
                if (File.Exists(oldthisFile))
                {
                    File.Delete(oldthisFile);
                }
            }
            
            if (File.Exists(thisFile))
            {
                DialogResult dr = XtraMessageBox.Show("检测名称重复,是否覆盖。", "确定要覆盖吗？", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }
            }
            using (StreamWriter file = File.CreateText(thisFile))
            {
                Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                serializer.Serialize(file, cl);
            }
            tn.SetValue("Key", this.textEdit1.Text);
        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {
            TextEdit te = (TextEdit)sender;
            xt.Text = te.Text;
            tn.SetValue("Key", te.Text);
        }
    }
}
